"use strict"
const mvc = require("cosjs.mvc");
const handle = mvc.library("handle","权限管理","admin/authorize")

exports.index = async function(){
    let level = this.session.get("level");
    if(level < 10){
        return this.error("权限不足")
    }
    this.view = "jqgrid";
    let data = {"title":'权限管理','view':'admin/authorize','handle':'admin/authorize'};

   let members = this.model.admin('members');
   let ret= await members.find({'level':{$lt:level}},{'multi':true,'projection':{'name':1,'uid':1}});
   data['members']=ret;
   let branchs= this.model.admin('branch');
   ret =await branchs.find({},{'multi':true,'projection':{'_id':1,'name':1}});
   data['branchs']=ret;
   return data;
}

exports.page = handle.page;
exports.save =async function(){
	let act=this.get('act','string');
	if(act=='add'){
		this.output = "json";
//		console.log(this.req.body);
//		let id=this.get('id','string');
//		let uid=this.get('uid','string');
		let info={};
		for(let k in this.req.body){
			if((k!='act')&&(k!='id')){
				info[k]=this.req.body[k];
			}
		}
		let name=this.get('name','string');
		let members = this.model.admin('members');
		let ret= await members.find({'name':name},{'projection':{'uid':1}});
		info.uid=ret['_id'];
		let app=this.get('branch','string');
		console.log(info);
		let amodel = this.model.admin('authorize');
		return amodel.insert(info);
	}
	else{

		return handle.save.call(this);
	}
}



